using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace FinanceAnalyzer
{
    public partial class MainForm : Form
    {
        //http://dotnet-snippets.de/dns/c-label-per-hover-erkennen-SID973.aspx
        private Dictionary<String, IMoneyFlow> _incomeReferenceList;
        private Dictionary<String, IMoneyFlow> _outcomeReferenceList;

        private DataTable _incomeSpreadsheetList;
        private DataTable _outcomeSpreadsheetList;

        private ArrayList _incomeDataTableDepth;
        private ArrayList _outcomeDataTableDepth;

        public MainForm()
        {
            InitializeComponent();

            _incomeReferenceList     = new Dictionary<string,IMoneyFlow>();
            _outcomeReferenceList    = new Dictionary<string, IMoneyFlow>();
            

            _incomeSpreadsheetList   = new DataTable("Income");
            _outcomeSpreadsheetList  = new DataTable("Outcome");

            _incomeSpreadsheetList.Columns.Add(new DataColumn("Name", typeof(String)));
            _incomeSpreadsheetList.Columns.Add(new DataColumn("Wert", typeof(double)));

            _outcomeSpreadsheetList.Columns.Add(new DataColumn("Name", typeof(String)));
            _outcomeSpreadsheetList.Columns.Add(new DataColumn("Wert", typeof(double)));

            dataGridViewIncome.DataSource = _incomeSpreadsheetList;
            dataGridViewOutcome.DataSource = _outcomeSpreadsheetList;


            _incomeDataTableDepth = new ArrayList(4);
            _incomeDataTableDepth.Add(null);
            _incomeDataTableDepth.Add(0);
            _incomeDataTableDepth.Add(null);
            _incomeDataTableDepth.Add(null);


            _outcomeDataTableDepth = new ArrayList(4);
            _outcomeDataTableDepth.Add(null);
            _outcomeDataTableDepth.Add(0);
            _outcomeDataTableDepth.Add(null);
            _outcomeDataTableDepth.Add(null);


            InWork inWork = InWork.UniqueInstance;
            inWork.LittleHelper();

            registerClass(inWork, "Work", EFinanceFlow.Income);

        }


        private void registerClass(IMoneyFlow classReference, String className, EFinanceFlow flow)
        {
            switch (flow)
            {
                case EFinanceFlow.Income:
                    _incomeReferenceList.Add(className, classReference);
                    break;
                case EFinanceFlow.Outcome:
                    _outcomeReferenceList.Add(className, classReference);
                    break;
            }
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            _incomeSpreadsheetList.Clear();
            _outcomeSpreadsheetList.Clear();
            treeViewIncome.Nodes.Clear();

            if (_incomeDataTableDepth[0] == null)
            {
                BuildLevel0(); 
            }
            else
            {
                switch ((int)_incomeDataTableDepth[1])
                {
                    case 1:
                        BuildLevel1();
                        break;
                    case 2:
                        BuildLevel2();
                        break;
                    case 3:
                        BuildLevel3();
                        break;
                    default:
                        throw new Exception("Unknown Leveldepth to draw");
                        break;
                }
                
            }

            BuildTree();
        }

        private void BuildTree()
        {
            //Income - Treeview
            foreach (String name in _incomeReferenceList.Keys)
            {
                treeViewIncome.BeginUpdate();
                treeViewIncome.Nodes.Add(name, name);
                List<DateTime> temp = _incomeReferenceList[name].getYears();
                for (int i = 0; i < temp.Count; i++)
                {
                    treeViewIncome.Nodes[name].Nodes.Add(temp[i].Year.ToString());
                }
                treeViewIncome.EndUpdate();
            }

            //Outcome - Treeview
            foreach (String name in _outcomeReferenceList.Keys)
            {
                treeViewIncome.BeginUpdate();
                treeViewIncome.Nodes.Add(name, name);
                List<DateTime> temp = _outcomeReferenceList[name].getYears();
                for (int i = 0; i < temp.Count; i++)
                {
                    treeViewIncome.Nodes[name].Nodes.Add(temp[i].Year.ToString());
                }

                treeViewIncome.EndUpdate();
            }
        }

        //All moneyflow objects
        private void BuildLevel0()
        {
            //In - DataView
            foreach (String name in _incomeReferenceList.Keys)
            {
                DataRow dr = _incomeSpreadsheetList.NewRow();
                dr[0] = name;
                dr[1] = _incomeReferenceList[name].Sum();
                _incomeSpreadsheetList.Rows.Add(dr);
            }

            //Out - DataView
            foreach (String name in _outcomeReferenceList.Keys)
            {
                DataRow dr = _outcomeSpreadsheetList.NewRow();
                dr[0] = name;
                dr[1] = _outcomeReferenceList[name].Sum();
                _outcomeSpreadsheetList.Rows.Add(dr);
            }        
        }

        //all years in a specific object
        private void BuildLevel1()
        {
            if (_incomeDataTableDepth[0] != null)
            {
                //In - DataView
                List<DateTime> tempIn = _incomeReferenceList[(String)_incomeDataTableDepth[0]].getYears();
                for (int i = 0; i < tempIn.Count; i++)
                {
                    DataRow dr = _incomeSpreadsheetList.NewRow();
                    dr[0] = tempIn[i].Year;
                    dr[1] = _incomeReferenceList[(String)_incomeDataTableDepth[0]].Sum(tempIn[i].Year.ToString());
                    _incomeSpreadsheetList.Rows.Add(dr);
                }
            }

            if (_outcomeDataTableDepth[0] != null)
            {
              
                //Out - DataView
                List<DateTime> tempOut = _outcomeReferenceList[(String)_outcomeDataTableDepth[0]].getYears();
                for (int i = 0; i < tempOut.Count; i++)
                {
                    DataRow dr = _outcomeSpreadsheetList.NewRow();
                    dr[0] = tempOut[i].Year;
                    dr[1] = _outcomeReferenceList[(String)_outcomeDataTableDepth[0]].Sum(tempOut[i].Year.ToString());
                    _outcomeSpreadsheetList.Rows.Add(dr);
                }
            }
        }

        //all month of a year in a specific object
        private void BuildLevel2()
        {

        
        }
        
        //all entries of month of a year in a specific object
        private void BuildLevel3()
        {


        }

        private void dataGridViewIncome_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                switch ((int)_incomeDataTableDepth[1])
                {
                    case 0:
                        _incomeDataTableDepth[0] = dataGridViewIncome.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        _incomeDataTableDepth[1] = 1;
                        _incomeDataTableDepth[2] = null;
                        _incomeDataTableDepth[3] = null;
                        break;
                    case 1:
                        _incomeDataTableDepth[0] = _incomeDataTableDepth[0];
                        _incomeDataTableDepth[1] = 2;
                        _incomeDataTableDepth[2] = dataGridViewIncome.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        _incomeDataTableDepth[3] = null;
                        break;
                    case 2:
                        _incomeDataTableDepth[0] = _incomeDataTableDepth[0];
                        _incomeDataTableDepth[1] = 3;
                        _incomeDataTableDepth[2] = _incomeDataTableDepth[2];
                        _incomeDataTableDepth[3] = dataGridViewIncome.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        break;
                    default:
                        throw new Exception("Leveldepth could not been acomplished on Cellclick");
                        break;
                }
            }
        }

        
    }
}